package com.becca.cake.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 时间转换工具类
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年5月24日 下午3:05:18
 */
public class DateUtils {
    private static Log              logger                        = LogFactory
                                                                      .getLog(DateUtils.class);
    public final static String      FORMAT_yyyy_MM_dd             = "yyyy-MM-dd";
    public final static String      FORMAT_yyyy_MM                = "yyyy-MM";
    public final static String      FORMAT_yyyy                   = "yyyy";
    public final static String      FORMAT_yyyy_M_d               = "yyyy-M-d";

    public final static String      FORMAT_yyyy_MM_dd_HH_mm_ss    = "yyyy-MM-dd HH:mm:ss";

    public final static String      FORMAT_yyyy_nian_MM_yue_mm_ri = "yyyy年MM月dd日";

    public final static String      FORMAT_yyyy_nian_M_yue_m_ri   = "yyyy年M月d日";
    public final static String      FORMAT_yyyyMMdd               = "yyyyMMddHH";

    private static SimpleDateFormat format                        = new SimpleDateFormat(
                                                                      FORMAT_yyyy_MM_dd_HH_mm_ss);
    public static final String      TIME_HOUR_Format              = "HH:00:00";
    public static final String      HMS_FORMAT                    = "HH:mm:ss";
    public static final String      TIME_CONNECT_FORMAT_STRING    = "yyyyMMddHHmmss";

    private static final DateFormat getFormat(String format) {
        return new SimpleDateFormat(format);
    }

    /**
     * 得到当前时间的字符串yyyy-MM-dd
     * @return String
     */
    public static String now2StrDate() {
        return now2Str(FORMAT_yyyy_MM_dd);
    }

    /**
     * 得到当前时间的字符串yyyy-MM
     * @return String
     */
    public static String now2StrMonthDate() {
        return now2Str("yyyy-MM");
    }

    /**
     * 得到当前时间的字符串yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String now2StrDateTime() {
        return now2Str(FORMAT_yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 得到当前时间的字符串
     * @param format 字符串格式
     * @return String
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String now2Str(String format) {
        return DateFormatUtils.format(new Date(), format);
    }

    /**
     * 字符串按格式转换为日期"yyyy-MM-dd"
     * 
     * @param sdate
     *          源字符串
     * @return
     */
    public static Date toShortDateTime(final String sdate) {
        return toDate(sdate, FORMAT_yyyy_MM_dd);
    }

    /**
     * 获取时间按字符序列
     * 
     * @author zhaoyingfei
     * @date Feb 15, 2012
     * @return
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取时间按字符序列
     * 
     * @author zhaoyingfei
     * @date Feb 15, 2012
     * @return
     */
    public static String getDateStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * Date转换到字符串yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String DateToString(Date date) {
        if (date == null) {
            return new String();
        }
        String dateStr = format.format(date);
        return dateStr;
    }

    public static Date StringToDate(String date) throws ParseException {
        Date newDate = null;
        if (date == null || date.trim().length() == 0) {
            return null;
        }
        try {
            newDate = format.parse(date);
        } catch (ParseException e) {
            throw new ParseException(date, 0);
        }

        return newDate;
    }

    public static String DateToString(Date date, String formater) {
        SimpleDateFormat format = new SimpleDateFormat(formater);
        return format.format(date);
    }

    /**
     * Date转换到字符串yyyy-MM-dd
     * @param date Date
     * @return String yyyy-MM-dd
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String date2StrDate(Date date) {
        return DateFormatUtils.format(date, FORMAT_yyyy_MM_dd);
    }

    /**
     * Date转换到字符串yyyy-MM
     * @param date Date
     * @return String yyyy-MM-dd
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String transYearMonth(Date date) {
        return DateFormatUtils.format(date, FORMAT_yyyy_MM);
    }

    /**
     * Date转换到字符串yyyy
     * @param date Date
     * @return String yyyy-MM-dd
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String transYear(Date date) {
        return DateFormatUtils.format(date, FORMAT_yyyy);
    }

    /**
     * Calendar转换到字符串yyyy-MM-dd
     * @param calendar Calendar
     * @return String yyyy-MM-dd
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String calendar2StrDate(Calendar calendar) {
        return date2StrDate(calendar.getTime());
    }

    /**
     * Date转换到字符串yyyyMMddHH
     * @param date Date
     * @return String yyyyMMddHH
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String createSerial(Date date) {
        return DateFormatUtils.format(date, FORMAT_yyyyMMdd);
    }

    /**
     * Calendar转换到字符串yyyyMMddHH
     * @param calendar Calendar
     * @return String yyyyMMddHH
     * @see org.apache.commons.lang.time.DateFormatUtils.DateFormatUtils#format(Date, String)
     */
    public static String createSerial(Calendar calendar) {
        return createSerial(calendar.getTime());
    }

    /**
     * @throws ParseException 
     * @Title: GetDateTimeStamp
     * @Description: 获取系统时间
     * @param @return 设定文件
     * @return Timestamp 返回类型
     * @throws
     */
    public static Date getDateStamp() throws ParseException {
        SimpleDateFormat iSimpleDateFormat = new SimpleDateFormat();
        iSimpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
        return iSimpleDateFormat.parse(iSimpleDateFormat.format(new Date()));

    }

    /**
     * 短时间格式()
     * @return
     * @throws ParseException
     */
    public static Date getDateShortStamp() throws ParseException {
        SimpleDateFormat iSimpleDateFormat = new SimpleDateFormat();
        iSimpleDateFormat.applyPattern("yyyy-MM-dd");
        return iSimpleDateFormat.parse(iSimpleDateFormat.format(new Date()));

    }

    /**
     * 格式化日期
     * 
     * @param myDate
     *          日期,Date类型
     * @param exmpl
     *          格式化表达式
     * @return 格式化字符串日期
     */
    public static String formatDate(Date myDate, String exmpl) {
        if (myDate == null) {
            return "";
        }
        if (exmpl == null || exmpl.equals("")) {
            return "";
        }
        String strDate = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(exmpl);
            strDate = formatter.format(myDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    /**
     * 字符串按格式转换为日期
     * 
     * @param sdate
     *          源字符串
     * @param format
     *          转换的格式
     * @return
     */
    public static Date toDate(final String sdate, final String format) {
        if (sdate == null || sdate.trim().equals("")) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date ret = null;
        try {
            ret = (Date) df.parseObject(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * @Title: parseDate
     * @Description: 返回指定的日期格式
     * @param @param dateString
     * @param @return 设定文件
     * @return Date 返回类型
     * @throws
     */
    public static Date parseDate(String dateString) {
        try {
            return StringToDate(dateString);
        } catch (ParseException e) {
            logger.error("", e);
        }
        return null;
    }

    /**
     * excel中数据格式化
     * @param date
     * @return
     */
    public static String excelDateFormat(Date date) {
        TimeZone zone = TimeZone.getTimeZone("GMT");
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_yyyy_MM_dd_HH_mm_ss);
        df.setTimeZone(zone);
        return df.format(date);
    }

    /**
     * 获得系统当前日期时间，以默认格式显示
     * 
     * @return e.g.2006-10-12 10:55:06
     */
    public static String getCurrentFormatDateTime() {
        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormator = new SimpleDateFormat(TIME_CONNECT_FORMAT_STRING);
        return dateFormator.format(currentDate);
    }

    /**
     * 字符串按格式转换为日期"yyyy-MM-dd HH:mm:ss"
     * @param sdate 源字符串
     * @return
     */
    public static Date toLongDateTime(final String date) {
        return toDate(date, FORMAT_yyyy_MM_dd_HH_mm_ss);
    }

    /**
     * 获取今天日期
     * @return
     */
    public static String getTodayDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yyyy_MM_dd);
        return sdf.format(date);
    }

    /**
     * 获取今天日期
     * @return
     */
    public static Date getDateToday() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_yyyy_MM_dd_HH_mm_ss);
        Date dateObj = null;
        try {
            dateObj = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateObj;
    }

    /**
     * 获取某天日期
     * @return
     */
    public static Date getSomeDayDate(int effectiveDay) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, effectiveDay);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_yyyy_MM_dd);
        String dateString = formatter.format(date);
        Date dateObj = null;
        try {
            dateObj = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateObj;
    }

    /**
     * yyyy-MM-dd
     * 
     * @param date
     * 
     * @return
     */
    public static final String dtSimpleFormat(Date date) {
        if (date == null) {
            return "";
        }

        return getFormat(FORMAT_yyyy_MM_dd).format(date);
    }

    public static String getOneTimeDate(int preOrLastDay, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_YEAR, preOrLastDay);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return DateToString(cal.getTime());
    }

    public static String getEnglishDateTime(Date dateTime) {
        DateFormat df = new SimpleDateFormat(" MMM dd yyyy", Locale.ENGLISH);
        return df.format(dateTime);
    }

    public static void main(String[] args) {

        System.out.println(DateUtils.getSomeDayDate(-1));
    }
}
